<script setup>
import { ref,onMounted } from 'vue'; 
import { queryAllDepts,addDept, getDeptById, editDept, deleteDeptById } from '@/api/dept';
import { ElMessage, ElMessageBox } from 'element-plus'

const tableData = ref([]);
const dialogTitle = ref("新增部门");
const dialogFormVisible = ref(false); 
const form = ref({name: ''});
const deptFormRef = ref(null);
const formRules = ref({
  name:[
    { required: true, message: '请输入部门名称', trigger: 'blur' },
    { min: 3, max: 20, message: '最少3个字符，最多20个字符', trigger: 'blur' },
  ]
});

// 显示新增窗口
function showAddWin(){
  dialogFormVisible.value = true; //显示新增窗口
  form.value = {}; //将窗口里的表单数据置空
}

//保存部门
async function save(){
  deptFormRef.value.validate(async valid=>{
    if(!valid){
      return;
    }

    let result;
    if (form.value.id) {
      result = await editDept(form.value);
    }else{
      result = await addDept(form.value);
    }

    if(result.code == 1){
      ElMessage.success('保存部门成功.') //弹窗提示操作成功
      dialogFormVisible.value = false; //隐藏dialog窗口
      form.value = {}; //重置新增表单
      search(); //重新搜索部门列表
    }else{
      ElMessage.error('保存部门失败.')
    }
  });
}

//查询部门列表
async function search(){
  //使用request发送请求，而不是原始的axios对象。得到的不是原始的response，而是被预处理后的响应正文result
  const result = await queryAllDepts();
  //从响应正文里取data，设置给数据tableData
  tableData.value = result.data;
}

// 操作处理函数 
const handleEdit = async (row) => {
  const result = await getDeptById(row.id);
  if (result.code==1) {
    form.value = result.data;
    dialogFormVisible.value=true;
    dialogTitle.value = "修改部门";
  }else{
    ElMessage.error("查询部门失败");
  }
};

const handleDelete = (row) => {
  //弹出确认窗口，如果确定要删除，则调用deleteDeptById函数进行删除
  ElMessageBox.confirm("此操作将永久删除该部门, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    //点击确定，则调用deleteDeptById方法进行删除
    const result = await deleteDeptById(row.id);
    if(result.code==1){
      ElMessage.success("删除部门成功");
      search(); //重新搜索部门列表
    }else{
      ElMessage.error("删除部门失败");
    }
  })
};

//钩子函数：页面一加载完成就立即发请求加载数据
onMounted(()=>{
  search();
});
</script>

<template>
  <!-- 新增按钮 -->
  <el-button type="primary" @click="showAddWin">新增部门</el-button> 
  <!-- 新增窗口/修改窗口 -->
  <el-dialog v-model="dialogFormVisible" :title="dialogTitle" width="500">
    <el-form :model="form" :rules="formRules" ref="deptFormRef">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 部门表格 -->
  <el-table :data="tableData" border style="width: 100%">
    <!-- 序号列 -->
    <el-table-column type="index" label="序号" width="80px" align="center" />
    <!-- 部门名称列 -->
    <el-table-column prop="name" label="部门名称" />
    <!-- 最后修改时间列 -->
    <el-table-column prop="updateTime" label="最后修改时间" />
    <!-- 操作列 -->
    <el-table-column label="操作"  align="center">
      <template #default="{ row }">
        <el-button type="primary" size="small" @click="handleEdit(row)">修改</el-button>
        <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>
 
<style scoped>
.el-button + .el-button {
  margin-left: 12px;
}
</style>